package fiberro;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Collections;
import java.util.Random;
import java.util.Vector;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;


public class MainWindow extends JFrame {
	public static final long serialVersionUID = 1;
	
	private JTextField rangeStart;
	private JTextField rangeEnd;
	private JTextField numbersCount;
	
	private JTextArea results_area;
	private JButton generateBtn;
	
	private GroupLayout layout;
	private JScrollPane sp;

	
	public MainWindow(int width, int height) {
		
	    Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
	    setLocation((size.width - width) / 2, (size.height - height) / 2);
	    setSize(new Dimension(width, height));
		   
	    layout = new GroupLayout(getContentPane());
	    getContentPane().setLayout(layout);

	    layout.setAutoCreateGaps(true);
	    layout.setAutoCreateContainerGaps(true);
	    
        addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent e) {
                dispose();
                System.exit(0);
            }
        });
        
        prepareWindow();      
	}
	
	private void prepareWindow() {
		rangeStart = new JTextField("1");					
		rangeEnd = new JTextField("49");
		numbersCount = new JTextField("6");
		
		results_area = new JTextArea("");
		results_area.setEditable(false);
		
		sp = new JScrollPane(results_area);
				
		JLabel odliczby = new JLabel("Start range:");		
		JLabel doliczby = new JLabel("End range:");
		JLabel ileliczb = new JLabel("Numbers?:");
		JLabel wyniki = new JLabel("Results:");
				
	    generateBtn = new JButton("Rand!");
	    	    
	    generateBtn.addActionListener(new ActionListener() {
	    	public void actionPerformed(ActionEvent e) {	    		
	    		generateNumbers();
	    	}
	    });
		
		layout.setHorizontalGroup(layout.createSequentialGroup()
			.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
				.addGroup(layout.createSequentialGroup()	
					.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
						.addComponent(odliczby, 50, 100, 100)
						.addComponent(doliczby)
						.addComponent(ileliczb)
					)		
					
					.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
						.addComponent(rangeStart)
						.addComponent(rangeEnd)
						.addComponent(numbersCount)				
					)
				)
				
				.addComponent(wyniki)
				.addComponent(sp)
				.addComponent(generateBtn)
			)
			
		);
		
		layout.setVerticalGroup(layout.createSequentialGroup()
			.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
				.addComponent(odliczby)
				.addComponent(rangeStart)
			)
			
			.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
				.addComponent(doliczby)
				.addComponent(rangeEnd)
			)	
			
			.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
				.addComponent(ileliczb)
				.addComponent(numbersCount)
			)
					
			.addComponent(wyniki)
			.addComponent(sp)
			.addComponent(generateBtn)			
		);		
	}
	
	private void generateNumbers() {
		String toAppend = new String("");		
		Random r = new Random(System.currentTimeMillis());		
		Vector<Integer> v = new Vector<Integer>();
		
		int start = 0;
		int end = 0;
		int count = 0; 
			
		try {
			start = Integer.valueOf(rangeStart.getText());
			end = Integer.valueOf(rangeEnd.getText());
			count = Integer.valueOf(numbersCount.getText());
		} catch(Exception e) {		
		}
		
		if (((start > 0) && (end > 0)) && ((end-start+1) >= count)) {
			for(int i=0; i < count; i++) {
				int val = (r.nextInt(end-start+1)+start);
				
				if (!v.contains(val)) {
					v.add(val);					
				} else {
					i--;
					continue;
				}
			}
			
			Collections.sort(v);
			
			for(int i=0; i < count; i++) {
				toAppend += ((i>0)?(", "):("")) + v.get(i);
			}
			
			toAppend += "\n";
		}
		
		results_area.append(toAppend);
	}
}
